home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / ghostscr / gsview07.zip / init.c < prev    next >
C/C++ Source or Header  |  1993-05-10  |  16KB  |  472 lines

  1. /*
  2.  * init.c   -- Initialisation functions for GSVIEW.EXE,
  3.  *             a graphical interface for MS-Windows Ghostscript
  4.  * Copyright (C) 1993  Russell Lang
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   Author: Russell Lang
  21.  * Internet: rjl@monu1.cc.monash.edu.au
  22.  */
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <mmsystem.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <dir.h>
  35. #define NeedFunctionPrototypes 1
  36. #include "ps.h"
  37. #include "gsview.h"
  38.  
  39. /* Open/Save File Dialog Box */
  40. OPENFILENAME ofn;
  41. char szOFilename[MAXSTR];    /* filename for OFN */
  42. char szOFilter[256];        /* filter for OFN */
  43. /* buttons */
  44. WNDPROC lpfnButtonWndProc;    /* default button WndProc */
  45. struct buttonlist {
  46.    HWND hbutton;
  47.    struct buttonlist *next;
  48. };
  49. struct buttonlist *buttonhead, *buttontail;
  50. int real_button_width;
  51.  
  52. /* Don't start another instance - use previous instance */
  53. void
  54. gsview_init0(LPSTR lpszCmdLine)
  55. {
  56.     HWND hwnd = FindWindow(szClassName, szAppName);
  57.     BringWindowToTop(hwnd);
  58.     if (lstrlen(lpszCmdLine) != 0) {
  59.         /* open file specified on command line */
  60.         LPSTR szFile = GlobalAllocPtr(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  61.         if (szFile) {
  62.         lstrcpy(szFile, lpszCmdLine);
  63.         PostMessage(hwnd, WM_COMMAND, IDM_DROP, (LPARAM)szFile);
  64.         }
  65.     }
  66. }
  67.  
  68. /* main initialisation */
  69. void
  70. gsview_init1(LPSTR lpszCmdLine)
  71. {
  72. WNDCLASS wndclass;
  73. WORD version = LOWORD(GetVersion());
  74. char *p;
  75.  
  76.     if ((LOBYTE(version)<<8) + HIBYTE(version) >= 0x30a)
  77.         is_win31 = TRUE;
  78.  
  79.     /* get path to help file */
  80.     GetModuleFileName(phInstance, szHelpName, sizeof(szHelpName));
  81.     if ((p = strrchr(szHelpName,'\\')) != (char *)NULL)
  82.         p++;
  83.     else
  84.         p = szHelpName;
  85.     LoadString(phInstance, IDS_HELPFILE, p, sizeof(szHelpName) - (p-szHelpName));
  86.  
  87.     /* help message for GetOpenFileName Dialog Box */
  88.     help_message = RegisterWindowMessage(HELPMSGSTRING);
  89.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  90.     
  91.     /* register the window class */
  92.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  93.     wndclass.lpfnWndProc = WndImgProc;
  94.     wndclass.cbClsExtra = 0;
  95.     wndclass.cbWndExtra = sizeof(LONG);
  96.     wndclass.hInstance = phInstance;
  97.     wndclass.hIcon = LoadIcon(phInstance,"gsview");
  98.     wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  99.     wndclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  100.     wndclass.lpszMenuName = NULL;
  101.     wndclass.lpszClassName = szClassName;
  102.     RegisterClass(&wndclass);
  103.  
  104.     /* defaults if entry not in gsview.ini */
  105.     strcpy(szGSwin, DEFAULT_GSCOMMAND);
  106.     img_origin.x = img_origin.y = CW_USEDEFAULT;
  107.     img_size.x = img_size.y = CW_USEDEFAULT;
  108.     xdpi = ydpi = DEFAULT_RESOLUTION;
  109.     settings = TRUE;
  110.     button_show = TRUE;
  111.     quick = TRUE;
  112.     timeout = DEFAULT_TIMEOUT;
  113.     save_dir = TRUE;
  114.     media = IDM_LETTER;
  115.     epsf_clip = FALSE;
  116.     orientation = IDM_PORTRAIT;
  117.     swap_landscape = FALSE;
  118.     hmenu = LoadMenu(phInstance, "gsview_menu");
  119.     haccel = LoadAccelerators(phInstance, "gsview_accel");
  120.     /* read entries from gsview.ini */
  121.     read_profile();
  122.  
  123.     hwndimg = CreateWindow(szClassName, (LPSTR)szAppName,
  124.           WS_OVERLAPPEDWINDOW,
  125.           img_origin.x, img_origin.y, 
  126.           img_size.x, img_size.y, 
  127.           NULL, NULL, phInstance, (void FAR *)NULL);
  128.  
  129.     /* load DLL for sounds */
  130.     if (is_win31) {
  131.         /* MMSYSTEM.DLL requires Windows 3.1, so to allow gsview to run
  132.            under Windows 3.0 we can't use the import library */
  133.         hlib_mmsystem = LoadLibrary("MMSYSTEM.DLL");
  134.         if (hlib_mmsystem >= HINSTANCE_ERROR) {
  135.         lpfnSndPlaySound = (FPSPS)GetProcAddress(hlib_mmsystem, "sndPlaySound");
  136.         }
  137.         else {
  138.         gserror(IDS_SOUNDNOMM, NULL, MB_ICONEXCLAMATION, -1);
  139.         hlib_mmsystem = (HINSTANCE)NULL;
  140.         }
  141.     }
  142.  
  143.     if (lstrlen(lpszCmdLine) >= 2) {
  144.         if ( ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-'))
  145.          &&  ((lpszCmdLine[1] == 'D') || (lpszCmdLine[1] == 'd')) ) {
  146.         debug = TRUE;
  147.         lpszCmdLine += 2;
  148.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  149.             lpszCmdLine++;
  150.         }
  151.      }
  152.     if (lstrlen(lpszCmdLine) != 0) {
  153.         /* open file specified on command line */
  154.         LPSTR szFile = GlobalAllocPtr(GHND, lstrlen(lpszCmdLine)+1);
  155.         if (szFile) {
  156.         lstrcpy(szFile, lpszCmdLine);
  157.         PostMessage(hwndimg, WM_COMMAND, IDM_DROP, (LPARAM)szFile);
  158.         }
  159.     }
  160.  
  161.     play_sound(SOUND_START);
  162. }
  163.  
  164. /* create gsview window menu bar and buttons */
  165. void
  166. gsview_create()
  167. {
  168. int i;
  169. char cReplace;
  170. WNDCLASS wndclass;
  171. HGLOBAL hglobal;
  172. int FAR *pButtonID;
  173. TEXTMETRIC tm;
  174. HDC hdc;
  175. HWND hbutton;
  176. WNDPROC    lpfnMenuButtonProc;
  177. POINT char_size;        /* size of default text characters */
  178. POINT button_size, button_shift;
  179.  
  180.     /* setup OPENFILENAME struct */
  181.     if (!LoadString(phInstance, IDS_FILTER, szOFilter, sizeof(szOFilter)-1))
  182.         return;
  183.     cReplace = szOFilter[strlen(szOFilter)-1];
  184.     for (i=0; szOFilter[i] != '\0'; i++)
  185.         if (szOFilter[i] == cReplace)
  186.         szOFilter[i] = '\0';
  187.     ofn.lStructSize = sizeof(OPENFILENAME);
  188.     ofn.hwndOwner = hwndimg;
  189.     ofn.lpstrFilter = szOFilter;
  190.     ofn.nFilterIndex = FILTER_PS;
  191.     ofn.lpstrFile = szOFilename;
  192.     ofn.nMaxFile = sizeof(szOFilename);
  193.     ofn.lpstrFileTitle = (LPSTR)NULL;
  194.     ofn.nMaxFileTitle = 0;
  195.     ofn.lpstrTitle = (LPSTR)NULL;
  196.     ofn.lpstrInitialDir = (LPSTR)NULL;
  197.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_SHOWHELP;
  198.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  199.  
  200.     /* add menu to image window */
  201.     SetMenu(hwndimg, hmenu);
  202.  
  203.     /* get default text size */
  204.     hdc = GetDC(hwndimg);
  205.     GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  206.     ReleaseDC(hwndimg,hdc);
  207.     char_size.x = tm.tmAveCharWidth;
  208.     char_size.y = tm.tmHeight;
  209.  
  210.     /* set size of info area, buttons and offset to child window */
  211.     info_rect.left = 0;
  212.     info_rect.right = info_rect.left + 60 * char_size.x;
  213.     info_rect.top = 0;
  214.     info_rect.bottom = char_size.y;
  215.     button_size.x = 24;
  216.     button_size.y = 24;
  217.     button_shift.x = 0;
  218.     button_shift.y = button_size.y - 1;
  219.     button_rect.top = info_rect.bottom;
  220.     button_rect.left = -1;
  221.     button_rect.right = button_size.x - 2;
  222.     button_rect.bottom = 0;        /* don't care */
  223.     real_button_width = button_rect.right;
  224.     if (!button_show)
  225.         button_rect.right = 0;
  226.     img_offset.x = button_rect.right + (button_show ? 1 : 0);
  227.     img_offset.y = info_rect.bottom + 1;
  228.     info_file.x = info_rect.left + 2;
  229.     info_file.y = 0;
  230.     info_page.x = info_rect.left + 34 * char_size.x + 2;
  231.     info_page.y = 0;
  232.     info_coord.left = info_rect.left + 20 * char_size.x;
  233.     info_coord.right = info_rect.left + 32 * char_size.x;
  234.     info_coord.top = 0;
  235.     info_coord.bottom = char_size.y;
  236.  
  237.     /* check menu items */
  238.     CheckMenuItem(hmenu, media, MF_BYCOMMAND | MF_CHECKED);
  239.     CheckMenuItem(hmenu, orientation, MF_BYCOMMAND | MF_CHECKED);
  240.     if (epsf_clip)
  241.         CheckMenuItem(hmenu, IDM_EPSFCLIP, MF_BYCOMMAND | MF_CHECKED);
  242.     if (epsf_warn)
  243.         CheckMenuItem(hmenu, IDM_EPSFWARN, MF_BYCOMMAND | MF_CHECKED);
  244.     if (swap_landscape)
  245.         CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
  246.     if (save_dir) 
  247.         CheckMenuItem(hmenu, IDM_SAVEDIR, MF_BYCOMMAND | MF_CHECKED);
  248.     if (button_show) 
  249.         CheckMenuItem(hmenu, IDM_BUTTONSHOW, MF_BYCOMMAND | MF_CHECKED);
  250.     if (quick) 
  251.         CheckMenuItem(hmenu, IDM_QUICK, MF_BYCOMMAND | MF_CHECKED);
  252.     if (redisplay) 
  253.         CheckMenuItem(hmenu, IDM_AUTOREDISPLAY, MF_BYCOMMAND | MF_CHECKED);
  254.     if (settings)
  255.         CheckMenuItem(hmenu, IDM_SAVESETTINGS, MF_BYCOMMAND | MF_CHECKED);
  256.  
  257.     hcWait = LoadCursor((HINSTANCE)NULL, IDC_WAIT);
  258.  
  259.     /* add buttons */
  260.     lpfnMenuButtonProc = (WNDPROC)MakeProcInstance((FARPROC)MenuButtonProc, phInstance);
  261.     GetClassInfo((HINSTANCE)NULL, "button", &wndclass);    /* get default button class info */
  262.     lpfnButtonWndProc = wndclass.lpfnWndProc;
  263.     
  264.     hglobal = LoadResource(phInstance, FindResource(phInstance, "gsview_button", RT_RCDATA));
  265.     if ( (pButtonID = (int FAR *)LockResource(hglobal)) == (int FAR *)NULL)
  266.         return;
  267.     
  268.     for (i=0; pButtonID[i]; i++) {
  269.         hbutton = CreateWindow("button", NULL,
  270.             WS_CHILD | (button_show ? WS_VISIBLE : 0) | BS_OWNERDRAW,
  271.             button_rect.left + i * button_shift.x,
  272.             button_rect.top  + i * button_shift.y,
  273.             button_size.x, button_size.y,
  274.             hwndimg, (HMENU)pButtonID[i],
  275.             phInstance, NULL);
  276.         SetWindowLong(hbutton, GWL_WNDPROC, (LONG)lpfnMenuButtonProc);
  277.         if (hbutton) {
  278.         if (buttonhead == (struct buttonlist *)NULL)
  279.             buttontail = buttonhead = (struct buttonlist *)malloc(sizeof(struct buttonlist));
  280.         else {
  281.             buttontail->next = (struct buttonlist *)malloc(sizeof(struct buttonlist)); 
  282.             buttontail = buttontail->next;
  283.         }
  284.         buttontail->hbutton = hbutton;
  285.         buttontail->next = NULL;
  286.         }
  287.     }
  288.     FreeResource(hglobal);
  289. }
  290.  
  291. void
  292. show_buttons(void)
  293. {
  294. struct buttonlist *bp = buttonhead;
  295. RECT rect;
  296.     button_rect.right = button_show ? real_button_width : 0;
  297.     img_offset.x = button_rect.right + (button_show ? 1 : 0);
  298.     if (!button_show) {
  299.         while (bp) {
  300.             ShowWindow(bp->hbutton, SW_HIDE);
  301.             bp = bp->next;
  302.         }
  303.         if (hwndimgchild == (HWND)NULL) {
  304.             GetClientRect(hwndimg, &rect);
  305.             rect.right = real_button_width + 1;
  306.             rect.top = button_rect.top;
  307.             InvalidateRect(hwndimg, &rect, TRUE);
  308.             UpdateWindow(hwndimg);
  309.         }
  310.     }
  311.     GetClientRect(hwndimg, &rect);
  312.     SetWindowPos(hwndimgchild, (HWND)NULL, rect.left+img_offset.x, rect.top+img_offset.y,
  313.         rect.right-img_offset.x, rect.bottom-img_offset.y, 
  314.         SWP_NOZORDER | SWP_NOACTIVATE);
  315.     rect.right = real_button_width + 1;
  316.     rect.top = button_rect.top;
  317.     if (button_show) {
  318.         InvalidateRect(hwndimg, &rect, FALSE);
  319.         UpdateWindow(hwndimg);
  320.         while (bp) {
  321.             ShowWindow(bp->hbutton, SW_SHOWNA);
  322.             bp = bp->next;
  323.         }
  324.     }
  325. }
  326.  
  327. /* read settings fron INI file */
  328. void
  329. read_profile()
  330. {
  331. int i;
  332. char profile[128];
  333. LPSTR file = INIFILE;
  334. LPSTR section = INISECTION;
  335.     GetPrivateProfileString(section, "Origin", "", profile, sizeof(profile), file);
  336.     if (sscanf(profile,"%d %d", &img_origin.x, &img_origin.y) != 2) {
  337.         img_origin.x = CW_USEDEFAULT;
  338.         img_origin.y = CW_USEDEFAULT;
  339.     }
  340.     GetPrivateProfileString(section, "Size", "", profile, sizeof(profile), file);
  341.     if (sscanf(profile,"%d %d", &img_size.x, &img_size.y) != 2) {
  342.         img_origin.x = CW_USEDEFAULT;
  343.         img_origin.y = CW_USEDEFAULT;
  344.     }
  345.     GetPrivateProfileString(section, "SaveSettings", "", profile, sizeof(profile), file);
  346.     if (sscanf(profile,"%d", &i) == 1)
  347.         settings = i;
  348.     GetPrivateProfileString(section, "ButtonBar", "", profile, sizeof(profile), file);
  349.     if (sscanf(profile,"%d", &i) == 1)
  350.         button_show = i;
  351.     GetPrivateProfileString(section, "Resolution", "", profile, sizeof(profile), file);
  352.     if (sscanf(profile,"%f %f", &xdpi, &ydpi) != 2) {
  353.         xdpi = DEFAULT_RESOLUTION;
  354.         ydpi = DEFAULT_RESOLUTION;
  355.     }
  356.     GetPrivateProfileString(section, "Media", "", profile, sizeof(profile), file);
  357.     if (strlen(profile)!=0) {
  358.         char medianame[20];
  359.         for (i=IDM_LETTER; i<IDM_USERSIZE; i++) {
  360.             GetMenuString(hmenu, i, medianame, sizeof(medianame), MF_BYCOMMAND);
  361.             if (!stricmp(medianame, profile)) {
  362.                 break;
  363.             }
  364.         }
  365.         media = i;
  366.         }
  367.     GetPrivateProfileString(section, "UserSize", "", profile, sizeof(profile), file);
  368.     if (sscanf(profile,"%d %d", &user_width, &user_height) != 2) {
  369.         /* this gives 640x480 pixels at 96dpi */
  370.         user_width = 480;
  371.         user_height = 360;
  372.     }
  373.     GetPrivateProfileString(section, "EpsfClip", "", profile, sizeof(profile), file);
  374.     if (sscanf(profile,"%d", &i) == 1)
  375.         epsf_clip = i;
  376.     GetPrivateProfileString(section, "EpsfWarn", "", profile, sizeof(profile), file);
  377.     if (sscanf(profile,"%d", &i) == 1)
  378.         epsf_warn = i;
  379.     GetPrivateProfileString(section, "Orientation", "", profile, sizeof(profile), file);
  380.     if (sscanf(profile,"%d", &i) == 1)
  381.         orientation = i+IDM_PORTRAIT;
  382.     GetPrivateProfileString(section, "SwapLandscape", "", profile, sizeof(profile), file);
  383.     if (sscanf(profile,"%d", &i) == 1)
  384.         swap_landscape = i;
  385.     GetPrivateProfileString(section, "QuickOpen", "", profile, sizeof(profile), file);
  386.     if (sscanf(profile,"%d", &i) == 1)
  387.         quick = i;
  388.     GetPrivateProfileString(section, "AutoRedisplay", "", profile, sizeof(profile), file);
  389.     if (sscanf(profile,"%d", &i) == 1)
  390.         redisplay = i;
  391.     GetPrivateProfileString(section, "Timeout", "", profile, sizeof(profile), file);
  392.     if (sscanf(profile,"%d", &i) == 1)
  393.         timeout = i;
  394.     if (timeout == 0)
  395.         timeout = 120;
  396.     GetPrivateProfileString(section, "SaveLastDir", "", profile, sizeof(profile), file);
  397.     if (sscanf(profile,"%d", &i) == 1)
  398.         save_dir = i;
  399.     if (save_dir) {
  400.         GetPrivateProfileString(section, "LastDir", "", profile, sizeof(profile), file);
  401.         if (!((strlen(profile)==2) && isalpha(profile[0]) && (profile[1]==':')))
  402.             chdir(profile);
  403.         if (isalpha(profile[0]) && (profile[1]==':'))
  404.         (void) setdisk(toupper(profile[0])-'A');
  405.     }
  406.     GetPrivateProfileString(section, "Ghostscript", "", profile, sizeof(profile), file);
  407.     if (profile[0] == '\0')
  408.         strcpy(szGSwin, DEFAULT_GSCOMMAND);
  409.     else
  410.         strcpy(szGSwin, profile);
  411.     GetPrivateProfileString(section, "Printer", ",", profile, sizeof(profile), file);
  412.     strcpy(device_name, strtok(profile,","));
  413.     strcpy(device_resolution, strtok(NULL, ","));
  414.     for (i=0; i<NUMSOUND; i++) {
  415.         GetPrivateProfileString(section, sound[i].entry, sound[i].file, profile, sizeof(profile), file);
  416.         strcpy(sound[i].file, profile);
  417.     }
  418. }
  419.  
  420. /* write settings to INI file */
  421. void
  422. write_profile()
  423. {
  424. char profile[MAXSTR];
  425. LPSTR file = INIFILE;
  426. LPSTR section = INISECTION;
  427. int i;
  428.     sprintf(profile, "%d %d", img_origin.x, img_origin.y);
  429.     WritePrivateProfileString(section, "Origin", profile, file);
  430.     sprintf(profile, "%d %d", img_size.x, img_size.y);
  431.     WritePrivateProfileString(section, "Size", profile, file);
  432.     sprintf(profile, "%d", settings);
  433.     WritePrivateProfileString(section, "SaveSettings", profile, file);
  434.     sprintf(profile, "%d", button_show);
  435.     WritePrivateProfileString(section, "ButtonBar", profile, file);
  436.     sprintf(profile, "%g %g", xdpi, ydpi);
  437.     WritePrivateProfileString(section, "Resolution", profile, file);
  438.     if (media == IDM_USERSIZE)
  439.         strcpy(profile, "User Defined");
  440.     else
  441.         GetMenuString(hmenu, media, profile, sizeof(profile), MF_BYCOMMAND);
  442.     WritePrivateProfileString(section, "Media", profile, file);
  443.     sprintf(profile, "%u %u", user_width, user_height);
  444.     WritePrivateProfileString(section, "UserSize", profile, file);
  445.     sprintf(profile, "%d", epsf_clip);
  446.     WritePrivateProfileString(section, "EpsfClip", profile, file);
  447.     sprintf(profile, "%d", epsf_warn);
  448.     WritePrivateProfileString(section, "EpsfWarn", profile, file);
  449.     sprintf(profile, "%d", orientation - IDM_PORTRAIT);
  450.     WritePrivateProfileString(section, "Orientation", profile, file);
  451.     sprintf(profile, "%d", swap_landscape);
  452.     WritePrivateProfileString(section, "SwapLandscape", profile, file);
  453.     sprintf(profile, "%d", quick);
  454.     WritePrivateProfileString(section, "QuickOpen", profile, file);
  455.     sprintf(profile, "%d", redisplay);
  456.     WritePrivateProfileString(section, "AutoRedisplay", profile, file);
  457.     sprintf(profile, "%d", timeout);
  458.     WritePrivateProfileString(section, "Timeout", profile, file);
  459.     if (save_dir) {
  460.         getcwd(profile, sizeof(profile));
  461.         WritePrivateProfileString(section, "LastDir", profile, file);
  462.     }
  463.     WritePrivateProfileString(section, "Ghostscript", szGSwin, file);
  464.     if (device_name[0] != '\0') {
  465.         sprintf(profile,"%s,%s",device_name,device_resolution);
  466.         WritePrivateProfileString(section, "Printer", profile, file);
  467.     }
  468.     for (i=0; i<NUMSOUND; i++)
  469.         WritePrivateProfileString(section, sound[i].entry, sound[i].file, file);
  470. }
  471.  
  472.